﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace WindowsFormsApplication3
{
    public partial class FormLogin : Form
    {
        string extPath = null;
        Configuration config = null;
        public FormLogin()
        {
            InitializeComponent();
            this.KeyPress += new KeyPressEventHandler(frmLogin_KeyPress);
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            extPath = Application.ExecutablePath;
            config = ConfigurationManager.OpenExeConfiguration(extPath);
            if (config.AppSettings.Settings["user"] != null)
            {
                tbUser.Text = config.AppSettings.Settings["user"].Value;
            }
            //ConfigurationSettings.AppSettings["user"];
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (tbUser.Text.ToLower() == "admin" && tbPwd.Text.ToLower() == "admin")
            {
                if (cbSaveUser.Checked)
                {
                    config.AppSettings.Settings.Remove("user");
                    config.AppSettings.Settings.Add("user", tbUser.Text);
                }
                config.Save(ConfigurationSaveMode.Modified);
                System.AppDomain.CurrentDomain.SetData("Succeed", true);
            }
            else
            {
                System.AppDomain.CurrentDomain.SetData("Succeed", false);
                MessageBox.Show("恭喜你，登录失败，请与网管联系！");
                tbUser.Text = "";
                tbPwd.Text = "";
                tbUser.Focus();
            }
        }


        private void tbUser_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (tbUser.Text != null && tbUser.Text != "")
                {
                    tbPwd.Focus();
                }
            }
        }

        private void tbPwd_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin.Focus();
                if (tbPwd.Text != null && tbPwd.Text != "")
                {
                    btnLogin.PerformClick();
                }
            }
        }

        void frmLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 27)
            {
                btnClose.PerformClick();
            }
        }


        //private void btnClose_Click(object sender, EventArgs e)
        //{
        //    Close();
        //}

        private void tbUserPass_TextChanged(object sender, EventArgs e)
        {
            btnLogin.Enabled =!string.IsNullOrEmpty(tbUser.Text) && !string.IsNullOrEmpty(tbPwd.Text);
            System.AppDomain.CurrentDomain.SetData("username", tbUser);
            System.AppDomain.CurrentDomain.SetData("password", tbPwd.Text.ToLower()); 
        }



        //private void tbPwd_TextChanged(object sender, EventArgs e)
        //{
        //    if (!string.IsNullOrEmpty(tbUser.Text) && !string.IsNullOrEmpty(tbPwd.Text))
        //    {
        //        btnLogin.Enabled = true;
        //    }
        //    else
        //    {
        //        btnLogin.Enabled = false;
        //    }
        //}

        public string UserName
        {
            get
            {
                return tbUser.Text.ToLower();
            }
            set
            {
                tbUser.Text = value;
            }
        }

        public string Password
        {
            get
            {
                return tbPwd.Text.ToLower(); 
            }
            set
            {
                tbPwd.Text = value;                
            }
        }

        public bool IsUserSaving
        {
            get
            {
                return cbSaveUser.Checked;
            }
        }

        public void SetFocus()
        {
            tbUser.Focus();
        }

        public string GetUserName()
        {
            return tbUser.Text.ToLower();
        }
    }
}
